import Vue from 'vue'
import Router from 'vue-router'
import stores from '../store'
import * as types from '../store/types'
import cookies from '../utils/cookies'

import Hello from 'components/hello'

import Main from 'components/main'
import Login from 'components/login'
import Reg from 'components/reg'
import myCustomer from 'components/myCustomer'
import myWithdrawals from 'components/myWithdrawals'
import myExchanges from 'components/myExchanges'
import bindCard from 'components/bindCard'
import accountMsg from 'components/accountMsg'
import accountEdit from 'components/accountEdit'
import bindSuccess from 'components/bindSuccess'
import messageCenter from 'components/messageCenter'
import messageDetails from 'components/messageDetails'
import accountCertification from 'components/accountCertification'
import accountSelect from 'components/accountSelect/accountSelect'
import individualAttst from 'components/individualAttst/individualAttst'
import individualSuccess from 'components/individualSuccess'
import individualError from 'components/individualError/individualError'
import zmxyAdd from 'components/zmxyAdd/zmxyAdd'


Vue.use(Router)
Vue.use(cookies)

const routes = [
  {
    path: '/Hello',
    name: 'Hello',
    component: Hello
  },
  {
    path: '/',
    name: 'name',
    redirect: '/Hello'
  },{
    path: '/login',
    name: 'login',
    component: Login
  },
  {
    path: '/reg',
    name: 'reg',
    component: Reg
  },
  {
    path: '/',
    name: 'home',
    redirect: '/main',
    meta: {requiresAuth: true}
  },
  {
    path: '/main',
    name: 'main',
    component: Main,
    meta: {requiresAuth: true}
  },
  { //我的客户
    path: '/myCustomer',
    name: 'myCustomer',
    component: myCustomer,
    meta: {requiresAuth: true}
  },
  { //我的提现记录
    path: '/myWithdrawals',
    name: 'myWithdrawals',
    component: myWithdrawals,
    meta: {requiresAuth: true}
  },
  { //我的兑换卡
    path: '/myExchanges',
    name: 'myExchanges',
    component: myExchanges,
    meta: {requiresAuth: true}
  },
  { //绑定兑换卡
    path: '/bind',
    name: 'bindCard',
    component: bindCard,
    meta: {requiresAuth: true}
  },
  { //显示账户信息
    path: '/accountMsg',
    name: 'accountMsg',
    component: accountMsg,
    meta: {requiresAuth: true}
  },
  { //修改、添加账户信息
    path: '/accountEdit',
    name: 'accountEdit',
    component: accountEdit,
    meta: {requiresAuth: true}
  },
  {
    //添加成功页面
    path: '/bindSuccess',
    name: 'bindSuccess',
    component: bindSuccess,
    meta: {requiresAuth: true}
  },
  {
    //消息中心
    path: '/messageCenter',
    name: 'messageCenter',
    component: messageCenter,
    meta: {requiresAuth: true}
  },
  {
    //认证信息显示
    path: '/accountCertification',
    name: 'accountCertification',
    component: accountCertification,
    meta: {requiresAuth: true}
  },{
    //认证选择
    path: '/accountSelect',
    name: 'accountSelect',
    component: accountSelect,
    meta: {requiresAuth: true}
  },
  {
    //芝麻信用认证
    path: '/zmxyAdd',
    name: 'zmxyAdd',
    component: zmxyAdd,
    meta: {requiresAuth: true}
  },
  {
    //个人证件认证
    path: '/individualAttst',
    name: 'individualAttst',
    component: individualAttst,
    meta: {requiresAuth: true}
  },
  {
    //消息中心详情页
    path: '/messageDetails/:msgId',
    name: 'messageDetails',
    component: messageDetails,
    meta: {requiresAuth: true}
  },
  {
    //消息中心详情页
    path: '/individualSuccess',
    name: 'individualSuccess',
    component: individualSuccess,
    meta: {requiresAuth: true}
  },
  {
    //消息中心详情页
    path: '/individualError',
    name: 'individualError',
    component: individualError,
    meta: {requiresAuth: true}
  }

];


if (localStorage.getItem('AppToken')) {
  stores.commit(types.LOGIN, window.localStorage.getItem('AppToken'))
}

const router = new Router({
  routes
});
router.beforeEach((to, from, next) => {
  if (to.matched.some(r => r.meta.requiresAuth)) {
    // console.log(stores.state.mutations.token)
    console.log(stores)
    if (stores.state.token) {
      next();
    }
    else {
      next({
        path: '/login',
        query: {redirect: to.fullPath}
      })
    }
  }
  else {
    next();
  }
})

export default router
